#!/usr/bin/env bash
# mindbender-report-task -- Run a given task on a report of the LATEST snapshot of the current DeepDive app
# > mindbender report-task REPORT_ID TASK [NAME=VALUE]...
#
# Author: Jaeho Shin <netj@cs.stanford.edu>
# Created: 2015-06-30
set -eu

# find path to the DeepDive app based on the snapshot path
DEEPDIVE_APP=$(find-deepdive-app)
export DEEPDIVE_APP
cd "$DEEPDIVE_APP"

[[ $# -ge 2 ]] || usage "$0" "Both REPORT_ID and TASK required"
ReportId=$1; shift
TaskName=$1; shift

(
# output the last reportId created
# FIXME this ugly hack can get wrong when tasks are run concurrently
numReports=$(wc -l <snapshot/LATEST/reports.ids)
while [[ $numReports -eq $(wc -l <snapshot/LATEST/reports.ids) ]]; do
    sleep 0.3
done
tail -n 1 snapshot/LATEST/reports.ids
) &
tailPid=$!
trap "kill $tailPid" ERR  # prevent from a bogus report id getting printed out when snapshot fails fast

# run the task under the LATEST snapshot
mindbender-snapshot <(
    escape-args-for-shell report-task "$ReportId" "$TaskName" "$@"
) snapshot/LATEST

trap - ERR
